﻿using System;
using System.Linq;
using System.Text;
using EnvDTE;

namespace Agile.VSIX {
    internal static partial class DTEExtensions {
        // Build.BuildSolution 编译
        // Edit.FindAllReferences 未使用的引用
        internal static void ExecCommand(this DTE dte,string commandName) {
            try {
                var command = FindCommand(dte,commandName);
                if(command != null && command.IsAvailable) {
                    dte.ExecuteCommand(command.Name,string.Empty);
                }
            } catch(Exception ex) {
                OutputWindowHelper.ExceptionWriteLine($"Unable to execute command(s) {string.Join(",",commandName)}",ex);
            }
        }
        internal static Command FindCommand(this DTE dte,params string[] commandNames) {
            if(commandNames == null || commandNames.Length == 0) return null;
            return dte.Commands.OfType<Command>().FirstOrDefault(x => commandNames.Contains(x.Name));
        }
        internal static EditPoint GetEditPointAtCursor(this TextDocument textDocument) {
            //获取指定文本文档的光标处的编辑点
            var cursor = textDocument.CreateEditPoint();
            cursor.MoveToPoint(textDocument.Selection.ActivePoint);
            return cursor;
        }
        internal static void JoinText(this TextSelection textSelection) {
            if(textSelection.IsEmpty) {
                textSelection.LineDown(true);
                textSelection.EndOfLine(true);
            }
            const string pattern = @"[ \t]*\r?\n[ \t]*";
            const string replacement = @" ";
            TextDocumentHelper.SubstituteAllStringMatches(textSelection,pattern,replacement);
            textSelection.CharRight();
        }
        internal static void SortText(this TextSelection textSelection) {
            if(textSelection.IsEmpty) {
                textSelection.LineDown(true);
                textSelection.EndOfLine(true);
            }
            var start = textSelection.TopPoint.CreateEditPoint();
            start.StartOfLine();
            var end = textSelection.BottomPoint.CreateEditPoint();
            if(!end.AtStartOfLine) {
                end.EndOfLine();
                end.CharRight();
            }
            var selectedText = start.GetText(end);
            var splitText = selectedText.Split(new[] { Environment.NewLine },StringSplitOptions.RemoveEmptyEntries);
            var orderedText = splitText.OrderBy(x => x);
            var sb = new StringBuilder();
            foreach(var line in orderedText) {
                sb.AppendLine(line);
            }
            var sortedText = sb.ToString();
            if(!selectedText.Equals(sortedText,StringComparison.CurrentCulture)) {
                start.Delete(end);
                var insertCursor = start.CreateEditPoint();
                insertCursor.Insert(sortedText);
                textSelection.MoveToPoint(start,false);
                textSelection.MoveToPoint(insertCursor,true);
            }
        }
    }
}